package sc.sniffer;

import java.io.IOException;

import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
import jpcap.packet.Packet;

public class Sniffer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// Obtain the list of network interfaces
		NetworkInterface[] devices = JpcapCaptor.getDeviceList();

		// for each network interface
		for (int i = 0; i < devices.length; i++) {
			// print out its name and description
			System.out.println(i + ": " + devices[i].name + "("
					+ devices[i].description + ")");

			// print out its datalink name and description
			System.out.println(" datalink: " + devices[i].datalink_name + "("
					+ devices[i].datalink_description + ")");

			// print out its MAC address
			System.out.print(" MAC address:");
			for (byte b : devices[i].mac_address)
				System.out.print(Integer.toHexString(b & 0xff) + ":");
			System.out.println();

			// print out its IP address, subnet mask and broadcast address
			for (NetworkInterfaceAddress a : devices[i].addresses)
				System.out.println(" address:" + a.address + " " + a.subnet
						+ " " + a.broadcast);
		}

		try {
			JpcapCaptor captor = JpcapCaptor.openDevice(devices[3], 65535,
					false, 10);
			captor.setFilter("udp and port 6111 or port 6112", true);
			
			captor.loopPacket(-1,new PacketPrinter());

			captor.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
